trees = {}
trees.texture = game.images:load("images/trees.png")
trees.texture:resize(32,32)
trees.texSizeX = 96
trees.texSizeY = 128

function newForest(sizeX, sizeY)
	local forest = {}
	forest = createMap(sizeX,sizeY)
	setForestTile(forest, 4, 4)
	setForestTile(forest, 5, 4)
	setForestTile(forest, 6, 4)
	setForestTile(forest, 7, 4)
	setForestTile(forest, 4, 5)
	setForestTile(forest, 5, 5)
	setForestTile(forest, 6, 5)
	setForestTile(forest, 7, 4)
	setForestTile(forest, 4, 6)
	setForestTile(forest, 5, 6)
	setForestTile(forest, 6, 6)
	setForestTile(forest, 7, 4)
	setForestTile(forest, 5, 3)
	setForestTile(forest, 6, 3)
	setForestTile(forest, 7, 3)
	setForestTile(forest, 8, 3)
	setForestTile(forest, 8, 4)
	setForestTile(forest, 7, 5)
	forest.minX = 0
	forest.minY = 0
	forest.maxX = sizeX
	forest.maxY = sizeY
	fitTiles(forest)
	return forest
end

--tile number of the forest
--7 8 9
--4 5 6
--1 2 3
function getForestTile(number)
	newtile = {}
	if(number == 1) then
		newtile = {x=0, y=0}
	elseif(number == 2) then
		newtile = {x=1, y=0}
	elseif(number == 3) then
		newtile = {x=2, y=0}
	elseif(number == 4) then
		newtile = {x=0, y=1}
	elseif(number == 5) then
		newtile = {x=1, y=1}
	elseif(number == 6) then
		newtile = {x=2, y=1}
	elseif(number == 7) then
		newtile = {x=0, y=2}
	elseif(number == 8) then
		newtile = {x=1, y=2}
	elseif(number == 9) then
		newtile = {x=2, y=2}
	else newtile = {x=0, y=3}
	end
	return newtile
end

function setForestTile(forest, mapPosX, mapPosY)
	forest[mapPosY][mapPosX].isSet = 1
end

function updateForest(forest)

end

--directions
--   c3 
--c4 X c2
--   c1 
function fitTiles(forest)
	for j=0,forest.maxX do
		for i=0, forest.maxY do
			if(forest[j]) then
				if(forest[j][i] and forest[j][i].isSet==1) then
					--c1,c2,c3,c4 = 0
					if(forest[j-1][i].isSet == 1) then c1 = 1 else c1=0 end
					if(forest[j][i+1].isSet == 1) then c2 = 1 else c2=0 end
					if(forest[j+1][i].isSet == 1) then c3 = 1 else c3=0 end
					if(forest[j][i-1].isSet == 1) then c4 = 1 else c4=0 end
						
					tile = 0
					if(c1==1 and c2==1 and c3==1 and c4==1) then tile = 5
					elseif(c1==0 and c2==1 and c3==1 and c4==0) then tile = 1
					elseif(c1==0 and c2==1 and c3==1 and c4==1) then tile = 2
					elseif(c1==0 and c2==0 and c3==1 and c4==1) then tile = 3
					elseif(c1==1 and c2==1 and c3==1 and c4==0) then tile = 4
					elseif(c1==1 and c2==0 and c3==1 and c4==1) then tile = 6
					elseif(c1==1 and c2==1 and c3==0 and c4==0) then tile = 7
					elseif(c1==1 and c2==1 and c3==0 and c4==1) then tile = 8
					elseif(c1==1 and c2==0 and c3==0 and c4==1) then tile = 9
					end
					
					--print(c1..c2..c3..c4)
					--print("tile: "..tile)
					newtile = {}
					newtile = getForestTile(tile)
					--print("y: "..newtile.y)
					forest[j][i].x = newtile.x
					forest[j][i].y = newtile.y
				end
			end
		end
	end
end